# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=jupyter_core
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.4.0
pkgrel=1
pkgdesc="Core common functionality of Jupyter projects. (mingw-w64)"
arch=('any')
url="https://jupyter.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")

source=("${_realname}-${pkgver}.tar.gz::https://github.com/jupyter/jupyter_core/archive/${pkgver}.tar.gz")
sha256sums=('a3c693cb4cd1251f887f034eba4b74e5ad1beab5baef43cc3ae9387450e72367')

all_build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=0

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python3-jupyter_core() {
  local interpreter=python3
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  all_build ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-jupyter_core() {
  local interpreter=python2
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  all_build ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  cd "${pkgdir}${MINGW_PREFIX}"/bin
  for _f in *; do
    mv ${_f} ${_f/%.*/}2${_f/${_f/%.*/}/}
  done
}

package_mingw-w64-i686-python2-jupyter_core() {
  package_python2-jupyter_core
}

package_mingw-w64-i686-python3-jupyter_core() {
  package_python3-jupyter_core
}

package_mingw-w64-x86_64-python2-jupyter_core() {
  package_python2-jupyter_core
}

package_mingw-w64-x86_64-python3-jupyter_core() {
  package_python3-jupyter_core
}
